<template>
	<view>
		<view class="title">当月销售</view>
		<!-- <span>{{formatDate}}</span> -->
		<view class="select-date">
			<view class="top">
				当前选择:
			</view>
			<view class="bottom">
				<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
					<view class="uni-input">{{date}}</view>
				</picker>
			</view>
			<view @click="inquire">查询</view>
		 </view>
		<view class="table">
			<scroll-view class="scroll" scroll-y="true" scroll-x="true" >
				<view class="head">
					<view class="head-item">商品名称</view>
					<view class="head-item">销售数量</view>
				</view>
				<view class="body" v-for="(item,index) in this.dyxl" :key='index'>
					<view class="body-item">{{item.spmc}}</view>
					<view class="body-item">{{item.dyxl}}</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	export default{
		data(){
			const currentDate = this.getDate({
				format: true
			})
			return{
				date: currentDate,
			}
		},
		
		computed:{
			...mapState(['goodsInfor','dyxl']),
			 startDate() {
					return this.getDate('start');
			  },
			  endDate() {
					return this.getDate('end');
			  },
			  
			formatDate(){
				return this.date.slice(0,4) + this.date.slice(5,7) + this.date.slice(8,10)
			}
		},
		
		onLoad(){
			console.log('onload',this.goodsInfor)
			// this.$store.dispatch('get_dyxl',this.formatDate)
		},
		
		methods:{
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			
			bindDateChange: function(e) {
				this.date = e.target.value
				console.log(this.date)
				// this.$store.dispatch('get_dyxl',this.formatDate)
			},
			
			inquire(){
				// this.$store.dispatch('get_dyxl',this.formatDate)
				this.$store.dispatch('get_dyxl','20201107')
			}
		}
	}
</script>

<style lang="scss">
	page{}
	.title{
		margin: 15rpx auto;
		font-weight:bold;
		text-align: center;
		font-size: 35rpx;
	}
	.select-date{
		display: flex;
		padding:0 20rpx;
		// border: 1rpx solid #f00;
		// background-color: #f2f2f2;
		.top{
			font-weight: bold;
			 // border: 1rpx solid #f00;
			 margin-right: 15rpx;
		}
		.bottom{
			flex: 1;
			// padding:0 20rpx;
			// border: 1rpx solid #00f;
			// border-top:1rpx solid #bbb ;
			border-bottom: 1rpx solid #bbb;
		}
	}
	.table{
		// border: 1rpx solid #f00;
		padding: 20rpx;
		.scroll{
			white-space: nowrap;
			border: 1rpx solid #bbb;
		}
		.head{
			display: flex;
			width: 100%;
		}
		.head-item{
			flex: 1;
			border: 1rpx solid #B2B2B2;
			text-align: center;
			margin: 3rpx;
			font-size: 25rpx;
			font-weight: bold;
			line-height: 80rpx;
			background-color: #fffaf0;
		}
		.body{
			display: flex;
			width: 100%;
		}
		.body-item{
			flex: 1;
			text-align: center;
			font-size: 25rpx;
			border: 1rpx solid #b2b2b2;
			margin: 3rpx;
			text-align: center;
			line-height: 70rpx;
		}
	}
</style>
